package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.oms.CheckSensitiveWordReq;
import com.homedo.odin.solution.contract.oms.CheckSensitiveWordResp;
import com.homedo.odin.solution.contract.oms.ImportReq;
import com.homedo.odin.solution.contract.oms.SensitiveWordListResp;
import com.homedo.odin.solution.contract.oms.UpdateWordReq;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.service.bizservice.oms.SensitiveWordBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhaoxudong
 * @date: 2018/3/7 14:11
 * @Description: 脏词管理
 */
@Api(description = "脏词管理", tags = {"OMS SensitiveWord"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/sensitive-word")
public class SensitiveWordController extends BaseController<SensitiveWordBizService> {


    @ApiOperation(value = "检查是否包含敏感词,并返回敏感词列表", notes = "检查是否包含敏感词,并返回敏感词列表", response = CheckSensitiveWordResp.class)
    @PostMapping("/result")
    public ResultData sensitiveWord(@RequestBody CheckSensitiveWordReq request) {
        CheckSensitiveWordResp resp = baseService.getSensitiveWord(request);
        if (resp.getText().isEmpty()) {
            return ResultData.newInstance();
        }
        return ResultData.newInstance(new Meta(ErrorCodeEnum.IsExist.getDbValue(), "包含敏感词"), resp);

    }

    @ApiOperation(value = "查询脏词列表", notes = "查询脏词列表", response = SensitiveWordListResp.class)
    @GetMapping
    public ResultData sensitiveWord(@ApiParam(value = "当前页码", required = true) @RequestParam("pageNum") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam("pageSize") Integer pageSize,
            @ApiParam(value = "搜索词", required = false) @RequestParam(value = "value", required = false) String value) {
        SensitiveWordListResp resp = baseService.findSensitiveList(pageNum, pageSize, value);
        logger.info("查询脏词列表返回结果:{}", resp.toJson());
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "导入脏词", notes = "导入脏词")
    @PostMapping
    public ResultData importWord(@RequestBody ImportReq request) {
        baseService.importSensitiveWord(request);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "修改脏词", notes = "修改脏词")
    @PutMapping
    public ResultData updateWord(@RequestBody UpdateWordReq request) {
        logger.info("修改脏词请求参数：{}", request.toJson());
        baseService.updateWord(request);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "删除脏词", notes = "删除脏词")
    @DeleteMapping("{value}/{userId}/{userName}")
    public ResultData deleteWord(@ApiParam(value = "脏词", required = true) @PathVariable("value") String value,
            @ApiParam(value = "用户ID", required = true) @PathVariable("userId") Long userId,
            @ApiParam(value = "用户名称", required = true) @PathVariable("userName") String userName) {
        baseService.updateWordStatus(value, userId, userName);
        return ResultData.newInstance();
    }
}
